package com.dense.kuiniu.bookborrow.business.home.back;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.dense.kuiniu.bookborrow.R;
import com.dense.kuiniu.bookborrow.base.BaseFragment;
import com.dense.kuiniu.bookborrow.business.adapter.BookBorrowOrReturnResultAdapter;
import com.dense.kuiniu.bookborrow.business.entity.QueryBookInfo;
import com.dense.kuiniu.bookborrow.receiver.SystemTTS;
import com.dense.kuiniu.bookborrow.view.CountDownView;
import com.dense.kuiniu.bookborrow.view.CounterController;
import com.dense.kuiniu.bookborrow.view.SpaceItemDecoration;
import com.dense.kuiniu.bookborrow.xjSetting.config.ShareKeyConfig;
import com.dense.kuiniu.bookborrow.xjSetting.utils.SharePreferceUtil;

import java.util.ArrayList;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author Administrator
 * @date 2020/8/13
 */
public class ReturnBookDelegate extends BaseFragment {
    private static final String BOOK_LIST = "list";

    public static ReturnBookDelegate getInstance(ArrayList<QueryBookInfo> list) {
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList(BOOK_LIST, list);
        ReturnBookDelegate delegate = new ReturnBookDelegate();
        delegate.setArguments(bundle);
        return delegate;
    }

    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.count_down_view)
    CountDownView mCountDownView;
    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R.id.tv_complete)
    TextView mTvComplete;

    private BookBorrowOrReturnResultAdapter mResultAdapter;
    private ArrayList<QueryBookInfo> mList;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mList = bundle.getParcelableArrayList(BOOK_LIST);
        }
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.delegate_return_book;
    }

    @Override
    protected void init() {
        if (getActivity() != null) {
            mTvTitle.setText(R.string.text_book_return);
            mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
            mRecyclerView.addItemDecoration(new SpaceItemDecoration(20));
            mRecyclerView.setAdapter(mResultAdapter = new BookBorrowOrReturnResultAdapter(R.layout.item_book_borrow_result, null, false));
            mResultAdapter.setEmptyView(R.layout.view_loading);
            int second = SharePreferceUtil.getInstance(getContext()).getInt(ShareKeyConfig.SAN_JI_CAIDAN,60);
            mCountDownView.setSeconds(second);
            mCountDownView.start(new CounterController.OnCompleteListener() {
                @Override
                public void onComplete() {
                    exit();
                }
            });
        }
    }

    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        mResultAdapter.setEmptyView(R.layout.view_empty);
        mResultAdapter.setNewInstance(mList);
        SystemTTS.getInstance().playText("还书完成，请拿走图书");
    }

    @OnClick({R.id.tv_return, R.id.tv_complete})
    public void onClickView(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.tv_return:
            case R.id.tv_complete:
                exit();
                break;
        }
    }


    private void exit() {
        pop();
    }

}